import { defineStore } from 'pinia';
import { ref, reactive } from 'vue';
import { login } from '../api/login';
// import { setToken, getToken, removeToken } from '../utils/auth'; // 你可以在 auth.js 文件中管理 token

export const useUserStore = defineStore('userStore', () => {

  const userState = reactive({
      token: '',
      id: '',
      username: '',
      nickname:'',
      email:'',
      tel: '',
      introduction:'',
      avatar: '',
      isMember: false,
      currentAudioIndex:0,
      playIndex: 0,
      isShowLogin: false,
      pageIndex:0,
      isCreateMusicComplete: false,
      creatingMusic: false,
      workListData:[],
      musicList: [],
      betaVersion: true,  //文字中是否显示“内测”
      uuid: '',
  })

  function resetUserState() {
    userState.token = ''
    userState.id = ''
    userState.username = ''
    userState.nickname = ''
    userState.email = ''
    userState.tel = ''
    userState.introduction = ''
    userState.avatar = ''
    userState.isMember = false
    userState.currentAudioIndex =0
    userState.playIndex = 0
    userState.isShowLogin = false
    userState.pageIndex = 0
    userState.creatingMusic = false
    userState.isCreateMusicComplete = false
    userState.uuid = ''
  }

    // state 定义
    // const token = ref(getToken() || ''); // 从 localStorage 获取 token，默认为空
    const token = ref( userState.token )
    const permissions = ref(null); // 用来存储权限数据

    // 登录方法
    const Login = async (formdata) => {
      try {
        const response = await login(formdata); // 调用登录 API
        
        if (response.code === '200') {
          console.log('登陆数据：', response);
          // console.log(JSON.stringify(response.data[0]));
          
          // 设置权限和用户 ID
          // setLocalPermission(JSON.stringify(response.data)); 
          // setLocalUid(response.data[0].uid);

          // 设置 token
          // token.value = 'token_128127122912102'; 
          // setToken(token.value); // 将 token 保存到 localStorage

          // permissions.value = response.data; // 存储权限数据

          // userState.token = 'a001'
          
          return response; // 返回响应
        } else {
          throw new Error('登录失败');
        }
      } catch (error) {
        throw new Error(error.message || '登录失败');
      }
    };

    function removeToken() {
      userState.token = '',
      userState.username = '',
      userState.isMember = false
    }

    // 登出方法
    const logout = () => {
      // token.value = ''; // 清空 token
      resetUserState()
      userState.isShowLogin = true
      // removeToken(); // 从 localStorage 中移除 token
    };

    // 定义一个方法 setUsername，用于更新用户名
    const setUsername = (newUsername) => {
      userState.username = newUsername;
    };

    // 定义一个方法 setToken，用于更新 token
    const setToken = (newToken) => {
      userState.token = newToken;
    };

    return {
      userState,
      token,
      permissions,
      setToken,
      setUsername,
      Login,
      logout,
      resetUserState
    };
  },{
    persist: {
      enabled: true,
      strategies: [
        {
          key: 'userStore',
          storage: localStorage,
          paths: [ 
              'userState.token', 
              'userState.id', 
              'userState.email', 
              'userState.tel',
              'userState.username', 
              'userState.isMember', 
              'userState.musicList', 
            ]
        }
      ]
    }
  }

);
